﻿using Microsoft.Extensions.Logging;
using Serilog.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Core.Info;
using Core.Log.SerilogExtend.Enricher;
using Core.Log.SerilogExtend;
using Microsoft.Extensions.Configuration;
using System.IO.Compression;

namespace Core.Log
{
    public static class MicrosoftLogger
    {
        /// <summary>
        /// 创建日志记录器
        /// </summary>
        /// <returns></returns>
        public static Microsoft.Extensions.Logging.ILogger CreateLogger(string categoryName = "")
        {
            var microsoftLogger = Factory.CreateLogger(categoryName); // creates an instance of ILogger
            return microsoftLogger;
        }

        /// <summary>
        /// 创建日志记录器
        /// </summary>
        /// <returns></returns>
        public static Microsoft.Extensions.Logging.ILogger CreateLogger<T>()
        {
            var microsoftLogger = Factory.CreateLogger<T>(); // creates an instance of ILogger            
            return microsoftLogger;
        }

        /// <summary>
        /// 创建日志记录器
        /// </summary>
        /// <returns></returns>
        public static Microsoft.Extensions.Logging.ILogger CreateLogger(Type type)
        {
            var microsoftLogger = Factory.CreateLogger(type); // creates an instance of ILogger
            return microsoftLogger;
        }

        /// <summary>
        /// 日志配置
        /// </summary>
        public static ILoggerFactory Factory
        {
            get
            {
                //读取配置文件
                var settingsJsonConfig = AppLogFacotry.LogConfiguration;
                //var settingsConfig = new LoggerConfiguration().ReadFrom.Configuration(settingsJsonConfig);
                string? settingsConfigMinimumLevel = settingsJsonConfig?["Logging.LogLevel.Default"];
                string serilogMinimumLevel = string.IsNullOrEmpty(settingsConfigMinimumLevel) ? "Information" : settingsConfigMinimumLevel;

                bool isEanbledConsole = settingsJsonConfig?["LoggerEnabledConsole"]?.ToLower() == "true" ? true : false;
                bool isEanbledDebugConsole = settingsJsonConfig?["LoggerEnabledDebugConsole"]?.ToLower() == "true" ? true : false;
                ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
                {
                    if (isEanbledConsole)
                    {
                        builder.AddConsole();
                    }
                    if (isEanbledDebugConsole)
                    {
                        builder.AddDebug();
                    }
                });
                return loggerFactory;
            }
        }
    }
}
